package org.example.panel.datapanel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import org.example.inout.*;
import org.example.model.*;
import org.example.operation.*;
import org.example.trans.*;

/**
 * @author lenovo
 */

public class TotalDataPanel extends JPanel {
    public TotalDataPanel(){
        //组件准备
        JButton totalData = new JButton("导出txt");
        JButton totalDataExcel = new JButton("导出excel");
        JButton totalDataFrame = new JButton("查看报表");
        JTable table = new JTable();
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        table.setPreferredScrollableViewportSize(new Dimension(450,220));
        Box totalbox = Box.createHorizontalBox();
        Box box = Box.createVerticalBox();
        //注册监视器
        totalData.addActionListener((ActionEvent e)->{
            JFileChooser fileChooser = new JFileChooser();
            if(fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION){
                File file = fileChooser.getSelectedFile();
                String[] paths = file.getAbsolutePath().split("[.]");
                if(!"txt".equals(paths[paths.length-1]) && !"TXT".equals(paths[paths.length-1])){
                    JOptionPane.showMessageDialog(null,
                            "文件必须为.txt文件", null, JOptionPane.WARNING_MESSAGE);
                }else{
                    CourseOperation.loadCourse();
                    ScoreOperation.loadScore();
                    InOut.write(TransSort.transSortWriteTotal(Save.getHashMapCourse(),
                            SortOperation.sortScore(), SortOperation.averageCourseScore()),
                            file.getAbsolutePath());
                    JOptionPane.showMessageDialog(null,
                            "导出成功", null, JOptionPane.WARNING_MESSAGE);
                }
            }
        });
        totalDataExcel.addActionListener((ActionEvent e)->{
            JFileChooser fileChooser = new JFileChooser();
            if(fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION){
                File file = fileChooser.getSelectedFile();
                String[] paths = file.getAbsolutePath().split("[.]");
                if(!"xlsx".equals(paths[paths.length-1]) && !"XLSX".equals(paths[paths.length-1])){
                    JOptionPane.showMessageDialog(null,
                            "文件必须为.xlsx文件", null, JOptionPane.WARNING_MESSAGE);
                }else{
                    CourseOperation.loadCourse();
                    ScoreOperation.loadScore();
                    InOutExcel.writeExcel(TransSort.transSortWriteTotal(
                            Save.getHashMapCourse(), SortOperation.sortScore(),
                            SortOperation.averageCourseScore()), file.getAbsolutePath());
                    JOptionPane.showMessageDialog(null,
                            "导出成功", null, JOptionPane.WARNING_MESSAGE);
                }
            }
        });
        totalDataFrame.addActionListener((ActionEvent e)->{
            table.setModel(GraphOperation.totalTable());
            validate();
        });
        //添加组件
        totalbox.add(totalData);
        totalbox.add(Box.createHorizontalStrut(5));
        totalbox.add(totalDataExcel);
        totalbox.add(Box.createHorizontalStrut(5));
        totalbox.add(totalDataFrame);
        box.add(totalbox);
        box.add(Box.createVerticalStrut(5));
        box.add(new JScrollPane(table));
        add(box);
    }
}
